package com.base.commonlib.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.base.commonlib.R;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by arvin on 2018/10/25.
 */

public class MuitChooseDialog<T> extends BottomSheetDialog {
    private String mTitle;
    private List<T> mData;
    private RecyclerView mRv;
    private Context mContext;
    private TextView mTvSure;
    private OnItemChooseListener mListener;
    private BaseAdapter<T> mAdapter;

    private Map<String,T> mCheckData = new HashMap<>();
    public MuitChooseDialog(@NonNull Context context, String title, List<T> data, OnItemChooseListener listener) {
        super(context);
        mTitle = title;
        mData = data;
        mListener=listener;
        mContext=context;

    }


    public MuitChooseDialog(@NonNull Context context, String title, OnItemChooseListener listener) {
        super(context);
        mTitle = title;
        mContext = context;
        mListener = listener;
    }

    public void setData(List<T> data)
    {
        mData=data;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.muihoose_dialog_layout);
        TextView tv_title = findViewById(R.id.tv_title);
        mRv = findViewById(R.id.rv);
        mTvSure = findViewById(R.id.sure);
        tv_title.setText(mTitle);
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MuitChooseDialog.this.dismiss();
                List<Object> list = new ArrayList<>();
                for (Map.Entry<String,T> no : mCheckData.entrySet()){
                    list.add(no.getValue());
                }
                mListener.onItemChoose(list);
            }
        });
        initRv();
        super.onCreate(savedInstanceState);
    }

    private void initRv() {
        mAdapter = new BaseAdapter<T>(mData, mContext) {

            @Override
            public void convert(BaseHolder holder, final T o, boolean isHeader, boolean isBottom) {
                TextView tv = holder.getView(R.id.tv);
                tv.setText(o.toString());
                final CheckBox box = holder.getView(R.id.cb_box);
                if (mCheckData.containsKey(o.toString())){
                    box.setChecked(true);
                }else {
                    box.setChecked(false);
                }
                box.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (box.isChecked()){
                            if (!mCheckData.containsKey(o.toString())){
                                mCheckData.put(o.toString(),o);
                            }
                        }else {
                            if (mCheckData.containsKey(o.toString())){
                                mCheckData.remove(o.toString());
                            }
                        }
                    }
                });
            }
        };
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {


            }
        });
        mAdapter.putNormalType(R.layout.item_muichoose_dialog);
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        mRv.setAdapter(mAdapter);
    }

    @Override
    public void show() {
        super.show();
    }

    public interface OnItemChooseListener
    {
        void onItemChoose(List<Object> o);
    }


}
